﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Zj.HttpCodeDemo.ConsoleApp.Samples
{
    internal class LambdaSample
    {



        private readonly static List<Student> _studentList = new List<Student>();
        private readonly static List<Student> _studentList_ = new List<Student>();
        static LambdaSample()
        {
            for (int i = 0; i < 9999; i++)
            {
                _studentList.Add(new Student(new Random().Next(1, 9999)
                    , new Random().Next(1, 70)
                    , new Random().Next(1, 10)));
                _studentList_.Add(new Student(new Random().Next(1, 9999)
                 , new Random().Next(1, 70)
                 , new Random().Next(1, 10)));
            }
        }

        internal static void Sample()
        {
            //_studentList.Where(s => s.Age > 23)
            //            .OrderByDescending(s => s.Age)
            //            .Distinct(new StudentEq())
            //            .ToList()
            //            .ForEach(s => Console.WriteLine($"\t{s.Name}\t{s.Age}"));
            //var b=_studentList.Any(s => s.Age < 20);
            //Console.WriteLine(b);

            //_studentList.SelectMany(s => s.Courses)
            //    .ToList()
            //    .ForEach(c => Console.WriteLine(c.Title));

            _studentList
                .OrderBy(s => s.Age)
                .GroupBy(s => s.ClassName)
                .Where(item => item.Average(s => s.Age) < 50)
                .ToList()
                .ForEach(g => Console.WriteLine($"Key:\t{g.Key},\tCount:\t{g.Count()},\tAge:{g.Average(s => s.Age)}"));

            //_studentList.Intersect(_studentList_, new StudentEq()).ToList()
            //    .ForEach(s => Console.WriteLine(s.Name));//Intersect.

            //_studentList.Union(_studentList_, new StudentEq()).ToList()
            //    .ForEach(s => Console.WriteLine(s.Name));//Union.

            //_studentList.Except(_studentList_,new StudentEq()).ToList()
            //   .ForEach(s => Console.WriteLine(s.Name));//Except.


            //var list = from student in _studentList
            //           where student.Age > 22
            //           orderby student.Age descending
            //           group student by student.ClassName;
            //list.ToList()
            //    .Where(item=>item.Average(s=>s.Age)<40)
            //    .ToList()
            //    .ForEach(item => Console.WriteLine($"名称：\t{item.Key},\t平均年龄：\t{item.Average(s=>s.Age)}"));
            //list.ToList().ForEach(s => Console.WriteLine($"名称：\t{s.Name},\t年龄：\t{s.Age},\t班级：\t{s.ClassName}"));
        }
    }

    internal class Student
    {
        public Student(int nameNo, int age, int classNo)
        {
            this.Name = $"张{nameNo}";
            this.Age = age;
            this.ClassName = $"{classNo}班";
            this.Courses = new List<Course>() { new Course() { Title = $"{this.Name}Course" } };
        }
        public string Name { get; set; }
        public int Age { get; set; }
        public string ClassName { get; set; }

        public List<Course> Courses { get; set; }

    }

    public class Course
    {
        public string Title { get; set; }
    }

    class StudentEq
        : IEqualityComparer<Student>
    {
        public bool Equals(Student x, Student y)
        {
            return x.Name.Equals(y.Name);
        }

        public int GetHashCode(Student obj)
        {
            return obj.Name.GetHashCode();
        }
    }
}